﻿using NDK.Motion.MachineManager;
using LightController;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace NDK.Motion.Frm
{
    public partial class Dlg_AddLight : Form
    {
        private Light _light = new Light();
        public Dlg_AddLight()
        {
            InitializeComponent();
        }

        public Light Light
        {
            get { return _light; }
            set { _light = value; }
        }

        private void btn_Confirm_BtnClick(object sender, EventArgs e)
        {
             string ChanelName = cb_Chanel.Text;
           
            ILightController lghController = MachineDataManager.Instance.LightControllerList.Find(l => l.Name == cb_LightControllerName.Text);
            if (lghController != null)
            {
                _light.LightController = lghController;
            }
            else
            {
                DialogResult = DialogResult.Cancel;
                return;
            }
          var Channel = lghController.ChanelList.Find(x => x.Name == ChanelName);
            if (Channel != null)
                _light.LightChanel = Channel;

            _light.Name = tb_LightName.Text.Trim();

            int val = 0;
            if (int.TryParse(num_Value.Text, out val) == true)
            {
                _light.Value = val;
            }
            else
            {
                DialogResult = DialogResult.Cancel;
                return;
            }

            DialogResult = DialogResult.OK;
        }

        private void Dlg_AddLight_Load(object sender, EventArgs e)
        {
            cb_LightControllerName.DisplayMember = "Name";
            cb_LightControllerName.DataSource = new BindingList<ILightController>(MachineDataManager.Instance.LightControllerList);
        }

        private void cb_LightControllerName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = cb_LightControllerName.Text;
            if (name == string.Empty)
            {
                return;
            }
            ILightController lghController = MachineDataManager.Instance.LightControllerList.Find(l => l.Name == name);

            cb_Chanel.DisplayMember = "Name";
            cb_Chanel.DataSource = new BindingList<LightChanel>(lghController.ChanelList);
        }
    }
}
